﻿namespace SchoolExercise
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    internal class Class : ICommentable
    {
        private List<Student> classStudents;
        private List<Teacher> classTeachers;
        private string title; // TODO:DONE must be unique
        private string comment;

        public Class(string titleOfTheClass)
        {
            this.classStudents = new List<Student>();
            this.classTeachers = new List<Teacher>();
            this.Title = titleOfTheClass;
        }

        public Class(string titleOfTheClass, string commentForTheClass)
            : this(titleOfTheClass)
        {
            this.Comment = commentForTheClass;
        }

        public List<Student> ClassStudents
        {
            get
            {
                return this.classStudents;
            }
        }

        public List<Teacher> ClassTeachers
        {
            get
            {
                return this.classTeachers;
            }
        }

        public string Title
        {
            get
            {
                return this.title;
            }

            private set
            {
                this.title = value;
            }
        }

        //// public List<Student> ClassStudents
        //// {
        ////     get
        ////     {
        ////         return this.classStudents;
        ////     }
        //// 
        ////     set
        ////     {
        ////         this.classStudents = value;
        //// 
        ////         // it's not working //
        ////         // var sortedStudentsByNameInEachClass = this.classStudents.OrderBy(x => x.Name).ToList();
        ////         // this.classStudents = sortedStudentsByNameInEachClass.ToList();
        ////     }
        //// }

        public string Comment
        {
            get
            {
                return this.comment;
            }

            set
            {
                this.comment = value;
            }
        }

        public void AddStudent(Student studentToAdd)
        {
            bool isNumberUnique = true;
            foreach (var student in this.classStudents)
            {
                if (studentToAdd.Name == student.Name)
                {
                    isNumberUnique = false;
                    break;
                }
            }

            if (isNumberUnique == false)
            {
                throw new ArgumentException("The number of the student must be unique. That one is already used");
            }
            else
            {
                this.classStudents.Add(studentToAdd);
            }
        }

        public void RemoveStudent(Student studentToRemove)
        {
            this.classStudents.Remove(studentToRemove);
        }

        public void AddTeacher(Teacher teacherToAdd)
        {
            this.classTeachers.Add(teacherToAdd);
        }

        public void RemoveTeacher(Teacher teacherToRemove)
        {
            this.classTeachers.Remove(teacherToRemove);
        }
    }
}